﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using braklimitu.Filters;
using braklimitu.Models;
using System.Data.SqlClient;
using System.Configuration;

namespace bezlimitu.Controllers
{
    public class SiteController : Controller
    {
        //
        // GET: /Site/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Recharge()
        {
            return View();
        }

        public ActionResult Prices()
        {
            return View();
        }

        public ActionResult FAQ()
        {
            return View();
        }

        public ActionResult Legal()
        {
            return View();
        }

        public ActionResult Contact(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Contact(MailModel model, string returnUrl)
        {
            //Wysłanie maila
            MailMessage message = new MailMessage();

            try
            {
                message.From = new MailAddress(model.From, model.From, System.Text.Encoding.UTF8);
                message.ReplyToList.Add(new MailAddress(model.From));
                message.To.Add(new MailAddress("braklimitu1@gmail.com"));

                message.Subject = model.Subject;
                message.Body = model.Body;
            }
            catch (Exception ex) {
                ModelState.AddModelError("", "Podany E-mail jest nie poprawny!");
                return RedirectToAction("Contact", "Site");
            }

            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("braklimitu1@gmail.com", "dupalala");
            client.Port = 587;
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;

            try
            {
                client.Send(message);
                return RedirectToAction("Index", "Home");
            }
            catch (Exception ex) {
                ModelState.AddModelError("", "Błąd serwera!");
                return RedirectToAction("Contact", "Site");
            }
            
        }

        public ActionResult SiteMap()
        {
            return View();
        }

        public ActionResult Regulamin()
        {
            return View();
        }

        public ActionResult Reviews(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public String Reviews(CommentModel model,string returnUrl)
        {   
            if(model.Note>5 && model.Note<1){
                return "Zła nota!";
            }

            if(model.Comment==null || model.Comment.Length==0){
                return "Pusty komentarz!";
            }

            try{
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);

            SqlCommand cmd = new SqlCommand(
            "select UserId from UserProfile where UserName = \'" + User.Identity.Name.ToString() + "\'", connection);
            cmd.Connection.Open();
            String temp_userID = cmd.ExecuteScalar().ToString();
            cmd.Connection.Close();

            cmd = new SqlCommand("insert into Comments values" +
                "( " + temp_userID + ", " + model.Note + ", \'" + model.Comment + "\')", connection);
            cmd.Connection.Open();
            cmd.ExecuteScalar();
            cmd.Connection.Close();
            }catch(Exception ex){
                return "Błąd bazy!";
            }
            return "Wysłano!";
        }

        public ActionResult Services()
        {
            return View();
        }

        public ActionResult Privacy()
        {
            return View();
        }

        public ActionResult Download() 
        {
            return View();
        }

    }
}
